package edu.zzu.daoImpl;

import edu.zzu.Entity.Region;
import edu.zzu.dao.RegionDao;
import edu.zzu.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: GaoPeng
 * @date: 2021/4/20 23:06
 */
@Component
public class RegionDaoImpl implements RegionDao {

    @Autowired
    private RegionMapper regionMapper;

    private Region reg=null;
    private String id_tmp="";

    @Override
    public Region selectRegionById(String id) {
        Region region = regionMapper.selectByPrimaryKey(id);
        if (region.getStage()==3) {
            id_tmp=id.substring(0,4)+"00";
            reg=regionMapper.selectByPrimaryKey(id_tmp);
            region.setCty(reg.getCty());

            id_tmp=id.substring(0,2)+"0000";
            reg=regionMapper.selectByPrimaryKey(id_tmp);
            region.setPrv(reg.getPrv());
        }

        if (region.getStage()==2) {
            id_tmp=id.substring(0,2)+"0000";
            reg=regionMapper.selectByPrimaryKey(id_tmp);
            region.setPrv(reg.getPrv());
        }
        return region;
    }

    @Override
    public String getRegionNameById(String id) {
        Region region = selectRegionById(id);
        StringBuilder regName=new StringBuilder();

        if(region.getStage()>0) regName.append(region.getPrv());
        if(region.getStage()>1) regName.append(region.getCty());
        if(region.getStage()>2) regName.append(region.getTwn());

        return String.valueOf(regName);
    }

    @Override
    public String getIdByPrv(String prv) {
        return regionMapper.selectRegionCodeByPrv(prv);
    }

    @Override
    public String getIdByCty(String cty) {
        return regionMapper.selectRegionCodeByCty(cty);
    }

    @Override
    public String getIdByTwn(String twn) {
        return regionMapper.selectRegionCodeByTwn(twn);
    }
}
